package com.bulletelc.apollopre.module.main.center.afterSale.detail;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.entity.ServiceOrderGoodBean;
import com.bulletelc.apollopre.framework.MVPBaseActivity;
import com.bulletelc.apollopre.module.display.product.details.ProductDetailsActivity;
import com.bulletelc.apollopre.utils.ToastUtil;
import com.bulletelc.apollopre.widget.FoolBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by CX on 2018/1/27.
 */

public class ServiceOrderDetailActivity  extends MVPBaseActivity<ServiceOrderDetailContract.View, ServiceOrderDetailPresenter>
        implements ServiceOrderDetailContract.View, View.OnClickListener,AdapterView.OnItemClickListener, FoolBar.ClickCallback {

    private TextView tvState, tvServiceOrderNo, tvServiceType, tvServiceReason, tvGoodsCount, tvGoodsTotal, tvReason;
    private ListView listView;
    private GoodsListAdapter adapter;
    private List<ServiceOrderGoodBean> mSogList;
    private FoolBar flBar;
    private Button btnCommit;
    private String serviceOrderId;

    @Override
    public int getLayoutRes() {
        return R.layout.service_order_detail_activity;
    }

    @Override
    public void assignViews() {
        flBar = findView(R.id.flBar);
        flBar.setTitle(this.getResources().getString(R.string.order_service_detail));
        tvServiceType = findView(R.id.tv_service_order_type);
        tvState = findView(R.id.tv_state);
        tvServiceOrderNo = findView(R.id.tv_service_order_no);
        tvServiceReason = findView(R.id.tv_service_reason);
        tvGoodsCount = findView(R.id.tv_goods_count);
        tvGoodsTotal = findView(R.id.tv_goods_total);
        tvReason = findView(R.id.tv_reason);
        listView = findView(R.id.lv_goods);
        btnCommit = findView(R.id.btn_commit);
    }

    @Override
    public void setListener() {
        flBar.setClickCallback(this);
        btnCommit.setOnClickListener(this);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        serviceOrderId = getIntent().getStringExtra("sodId");
        mSogList = new ArrayList<>();
        adapter = new GoodsListAdapter(this, mSogList);
        listView.setAdapter(adapter);
        presenter.getServiceOrderDetail(getIntent().getStringExtra("sodId"));
    }

    @Override
    public void onSuccessGetServiceOrderDetail(List<ServiceOrderGoodBean> sogList, JSONObject obj) {
        try {
            tvServiceType.setText("退款类型：" + (obj.getInt("apply_type") == 20? "仅退换货":"退款退货"));
            tvState.setText("退款进度：" + getState(obj.getInt("state")));
            tvServiceOrderNo.setText("售后单号：" + obj.getString("code"));
            tvServiceReason.setText("退款理由：" + getReasonType(obj.getInt("reason_type")));
            tvReason.setText("退款理由：" + obj.getString("reason"));
            tvGoodsCount.setText("退款数量："+ sogList.get(0).getQuantity());
            tvGoodsTotal.setText("退款金额：¥" + sogList.get(0).getReturn_price());
            mSogList.clear();
            mSogList.addAll(sogList);
            adapter.notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private String getReasonType(int type) {
        switch (type) {
            case 10:
                return "质量问题";
            case 20:
                return "漏发或产品损坏";
            case 30:
                return "实物与描述不相符";
            case 40:
                return "其他原因";
        }
        return null;
    }

    private String getState(int status) {
        switch (status) {
            case 10:
                btnCommit.setEnabled(false);
                return "待处理";
            case 20:
                btnCommit.setEnabled(false);
                return "售后处理中";
            case 30:
                btnCommit.setVisibility(View.VISIBLE);
                btnCommit.setText("确认完成");
                return "卖家确认";
            case 60:
                return "拒绝";
            case 70:
                btnCommit.setText("买家已确认");
                btnCommit.setEnabled(false);
                return "买家确认完成";
            case 80:
                btnCommit.setText("售后关闭");
                btnCommit.setEnabled(false);
                return "卖家／买家关闭工单";
        }
        return null;
    }

    @Override
    public void onErrorGetServiceOrderDetail(String msg) {
        ToastUtil.showLongToast(this,R.string.network_msg);
    }

    @Override
    public void onFailGetServiceOrderDetail(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSuccessPostConfirm(boolean state) {
        if (state) {
            Toast.makeText(this, "操作成功", Toast.LENGTH_SHORT).show();
            btnCommit.setText("已确认");
            btnCommit.setBackgroundColor(getResources().getColor(R.color.province_line_border));
            btnCommit.setEnabled(false);
        }
    }

    @Override
    public void onErrorPostConfirm(String msg) {
        ToastUtil.showLongToast(this,R.string.network_msg);
    }

    @Override
    public void onFailPostConfirm(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected ServiceOrderDetailPresenter createPresenter() {
        return new ServiceOrderDetailPresenter();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_commit:// 买家确认
                //LogUtil.i("点击了");
                AlertDialog dialog = new AlertDialog.Builder(this)
                        .setMessage("确认售后处理完成了吗")
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                presenter.postConfirm(serviceOrderId);
                            }
                        })
                        .setNegativeButton("取消", null)
                        .create();
                dialog.show();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(this, ProductDetailsActivity.class);
        intent.putExtra(IntentCode.EXTRA_KEY_PRODUCT_ID, String.valueOf(mSogList.get(i).getProduct_id()));
        startActivity(intent);
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }
}
